package cn.tedu.serviceEdu.service.impl;

import cn.tedu.serviceBase.exception.EducationException;
import cn.tedu.serviceEdu.entity.EduCourse;
import cn.tedu.serviceEdu.entity.EduCourseDescription;
import cn.tedu.serviceEdu.entity.EduVideo;
import cn.tedu.serviceEdu.mapper.EduCourseMapper;
import cn.tedu.serviceEdu.query.*;
import cn.tedu.serviceEdu.service.EduChapterService;
import cn.tedu.serviceEdu.service.EduCourseDescriptionService;
import cn.tedu.serviceEdu.service.EduCourseService;
import cn.tedu.serviceEdu.service.EduVideoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author hong
 * @since 2023-05-18
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduCourseDescriptionService descriptionService;
    @Autowired
    private EduVideoService videoService;
    @Autowired
    private EduChapterService chapterService;

    @Override
    @Transactional
    public String saveCourse(CourseInfo courseInfo) {
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfo,eduCourse);
        boolean save = this.save(eduCourse);
        if(!save){
            throw new EducationException(20001,"课程添加失败");
        }
        String courseId = eduCourse.getId();
        EduCourseDescription description = new EduCourseDescription();
        description.setDescription(courseInfo.getDescription());
        description.setId(courseId);
        boolean saveDescription = descriptionService.save(description);
        if (!saveDescription) {
            throw new EducationException(20001,"课程描述添加失败");
        }
        return courseId;
    }

    @Override
    public CourseInfo getCourseInfo(String courseId) {
        if(courseId.isEmpty()){
            throw new EducationException(20001,"课程不存在");
        }
        EduCourse course = this.getById(courseId);
        CourseInfo courseInfo = new CourseInfo();
        BeanUtils.copyProperties(course, courseInfo);
        EduCourseDescription description = descriptionService.getById(course.getId());
        courseInfo.setDescription(description.getDescription());
        return courseInfo;
    }

    @Override
    public void updateCourse(CourseInfo courseInfo) {
        String courseInfoId = courseInfo.getId();
        EduCourse course = new EduCourse();
        BeanUtils.copyProperties(courseInfo,course);
        boolean update = this.updateById(course);
        if (!update) {
            throw new EducationException(20001,"课程信息修改失败");
        }
        EduCourseDescription description = descriptionService.getById(courseInfoId);
        boolean updateById = descriptionService.updateById(description);
        if (!updateById) {
            throw new EducationException(20001,"课程简介修改失败");
        }
    }

    @Override
    public CoursePublishVo getCoursePublishVoById(String id) {
        CoursePublishVo publishVo = baseMapper.selectCoursePublishVoById(id);
        return publishVo;
    }

    @Override
    public void publishCourseById(String id) {
        EduCourse course = new EduCourse();
        course.setId(id);
        course.setStatus("Normal");
        Integer count = baseMapper.updateById(course);
        if(count == 0){
            throw new EducationException(20001,"课程发布失败");
        }
    }

    @Override
    public void pageQuery(Page<EduCourse> pageParam, CourseQuery courseQuery) {

        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");

        if (courseQuery == null){
            baseMapper.selectPage(pageParam, queryWrapper);
            return;
        }

        String title = courseQuery.getTitle();
        String teacherId = courseQuery.getTeacherId();
        String subjectParentId = courseQuery.getSubjectParentId();
        String subjectId = courseQuery.getSubjectId();

        if (!StringUtils.isEmpty(title)) {
            queryWrapper.like("title", title);
        }

        if (!StringUtils.isEmpty(teacherId) ) {
            queryWrapper.eq("teacher_id", teacherId);
        }

        if (!StringUtils.isEmpty(subjectParentId)) {
            queryWrapper.ge("subject_parent_id", subjectParentId);
        }

        if (!StringUtils.isEmpty(subjectId)) {
            queryWrapper.ge("subject_id", subjectId);
        }

        baseMapper.selectPage(pageParam, queryWrapper);
    }

    @Override
    public boolean removeCourseById(String id) {

        //根据id删除所有视频
        videoService.removeByCourseId(id);

        //根据id删除所有章节
        chapterService.removeByCourseId(id);

        Integer result = baseMapper.deleteById(id);

        descriptionService.removeById(id);
        return null != result && result > 0;
    }

    @Override
    public List<EduCourse> getIndexCourseList() {
        LambdaQueryWrapper<EduCourse> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(EduCourse::getViewCount,EduCourse::getBuyCount).last("limit 8");
        return this.list(wrapper);
    }

    @Override
    public Map<String, Object> pageListWeb(Page<EduCourse> pageParam, FrontCourseQueryVo courseQuery) {
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(courseQuery.getSubjectParentId())) {
            queryWrapper.eq("subject_parent_id", courseQuery.getSubjectParentId());
        }

        if (!StringUtils.isEmpty(courseQuery.getSubjectId())) {
            queryWrapper.eq("subject_id", courseQuery.getSubjectId());
        }

        if (!StringUtils.isEmpty(courseQuery.getBuyCountSort())) {
            queryWrapper.orderByDesc("buy_count");
        }

        if (!StringUtils.isEmpty(courseQuery.getGmtCreateSort())) {
            queryWrapper.orderByDesc("create_time");
        }

        if (!StringUtils.isEmpty(courseQuery.getPriceSort())) {
            queryWrapper.orderByDesc("price");
        }

        baseMapper.selectPage(pageParam, queryWrapper);

        List<EduCourse> records = pageParam.getRecords();
        long current = pageParam.getCurrent();
        long pages = pageParam.getPages();
        long size = pageParam.getSize();
        long total = pageParam.getTotal();
        boolean hasNext = pageParam.hasNext();
        boolean hasPrevious = pageParam.hasPrevious();

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        return map;

    }

    @Override
    public FrontCourseWebVo selectInfoWebById(String courseId) {
        EduCourse course = baseMapper.selectById(courseId);
        course.setViewCount(course.getViewCount() + 1);
        baseMapper.updateById(course);
        return baseMapper.selectInfoWebById(courseId);

    }

    @Override
    public void updatePageViewCount(String id) {
        EduCourse course = baseMapper.selectById(id);
        course.setViewCount(course.getViewCount() + 1);
        baseMapper.updateById(course);
    }

    @Override
    public List<EduCourse> selectByTeacherId(String id) {
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<EduCourse>();

        queryWrapper.eq("teacher_id", id);
        //按照最后更新时间倒序排列
        queryWrapper.orderByDesc("modified_time");

        List<EduCourse> courses = baseMapper.selectList(queryWrapper);
        return courses;
    }
}
